import  request  from '@/utils/request'
import {defineStore} from 'pinia'
import {ApiRes, BannerItem, newGoodsItem, HotGoods, Brand} from '@/types/data'
const useHomeStore = defineStore("home", {
    // 存储数据
    state() {
        return {
            // 轮播图数据列表,类型断言
            bannerList : [] as BannerItem[],
            // 新鲜好物列表
            newGoodsList : []  as  newGoodsItem[],
            // 人气推荐列表
            hotGoodsList : [] as HotGoods[],
            // 热门品牌列表
            brandList: [] as Brand[],
        }
    },
    // 计算属性
    getters : {

    },
    // 修改state数据
    actions : {
        // 获取首页轮播图数据
       async getBannerList() {
            const res = await request.get<ApiRes<BannerItem[]>>("/home/banner")
            // console.log(res)
            this.bannerList = res.data.result
        },

        // 获取新鲜好物的数据
        async getNewGoodsList() {
            setTimeout(async() => {
                const res = await request.get<ApiRes<newGoodsItem[]>>("/home/new")
                // console.log(res)
                this.newGoodsList = res.data.result
            },3000);
            // const res = await request.get<ApiRes<newGoodsItem[]>>("/home/new")
            // // console.log(res)
            // this.newGoodsList = res.data.result
        },
        // 获取人气推荐的数据
        async getHotGoodsList() {
            setTimeout(async() => {
                const res = await request.get<ApiRes<HotGoods[]>>("/home/hot")
               // console.log(res)
               this.hotGoodsList = res.data.result
            },3000);
            // const res = await request.get<ApiRes<HotGoods[]>>("/home/hot")
            //  console.log(res)
            // this.hotGoodsList = res.data.result
        },

        // 获取热门品牌的数据
        async getBrandList() {
            const res = await request.get<ApiRes<Brand[]>>("/home/brand")
           // console.log(res)
            this.brandList = res.data.result
        }
    }
})

export default useHomeStore